package com.etone.app.crawler.portals.common;

import com.etone.app.crawler.portals.entity.Task;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.log4j.Logger;

/**
 *
 * @author <a href="mailto:cbean@qq.com">CowboyBean</a>
 * @date Mar 14, 2014
 * @version $$Reversion$$
 */
public class TaskQueue {

    private static final BlockingQueue<Task> queue = new LinkedBlockingQueue<>();

    private static final Logger logger = Logger.getLogger(TaskQueue.class);

    public static synchronized Task getTask() {

        Task task = null;
        try {
            task = queue.take();
            logger.info("提取了一个任务 ---> " + task.getUrl());
        } catch (Exception ex) {
            logger.error("获取任务失败！", ex);
        }
        return task;
    }

    public static synchronized void putTask(Task task) {

        try {
            queue.put(task);
            logger.info("插入了一个任务 ---> " + task.getUrl());
        } catch (Exception ex) {
            logger.error("添加任务失败！", ex);
        }

    }

}
